﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using DAL;

namespace logic
{
    public class orderItem
    {
        public static DataSet select(string where, string orderby, int pagesize, int pageindex)
        {
            return DAL.DbHelper.ExecuteDataSetPageSize("oiid,productId,proName,price,unit,NatureCode,initQuantity,quantity,note,isShowDeduct,status,amount,inputDt,orderno,sellerName,sellerNo", "vw_OrderItem_list", where, "oiid", orderby, pagesize, pageindex);
        }
        public static DataSet selectUnsale(string where, string orderby, int pagesize, int pageindex)
        {
            return DAL.DbHelper.ExecuteDataSetPageSize("oiId,productId,name,price,unit,businessnaturename,initQuantity,quantity,note", "vw_DynamicOrderItem_listWithisOnsale", where, "oiId", orderby, pagesize, pageindex);
        }
        public static void insert(OrderItemEntity _orderItem)
        {
            OrderItem.insert(_orderItem);
        }
        public static void insertDynamicOrderItem(OrderItemEntity _orderItem)
        {
            OrderItem.insertDynamicOrderItem(_orderItem);
        }
        public static void update(OrderItemEntity _orderItem)
        {
            OrderItem.update(_orderItem);
        }
        public static void updateQuantity(int oiid, decimal quantity, string note)
        {
            OrderItem.updateQuantity(oiid, quantity, note);
        }
        public static void updateDynamicOrderItem(int orderid, int productid, string businessnature)
        {
            OrderItem.updateDynamicOrderItem(orderid, productid, businessnature);
        }
        public static void updateDynamicOrderItemByOiid(int oiid, string note, decimal quantity)
        {
            OrderItem.updateDynamicOrderItemByOiid(oiid, note, quantity);
        }
        public static void delete(int oiid)
        {
            OrderItem.delete(oiid);
        }
        public static void deleteDynamicOrderItem(int oiid)
        {
            OrderItem.deleteDynamicOrderItem(oiid);
        }
        public static DataTable listWithSaleReport(string where)
        {
            return OrderItem.listWithSaleReport(where);
        }
        public static DataSet listByCategory(string where, string orderby, int pagesize, int pageindex)
        {
            return DAL.DbHelper.ExecuteDataSetPageSize("*", "vw_OrderItem_listByCategory", where, "oiid", orderby, pagesize, pageindex);
        }

        /// <summary>
        /// 供应商查看分类统计
        /// </summary>
        /// <param name="where">条件中带有Group By</param>
        /// <param name="orderby"></param>
        /// <param name="pagesize"></param>
        /// <param name="pageindex"></param>
        /// <returns></returns>
        public static DataSet listByCategoryWithSupply(string where, string orderby, int pagesize, int pageindex)
        {
            return DAL.DbHelper.ExecuteDataSetPageSize("productId,proName,standard,sum(quantity) as Quantity,unit,avg(sellerPrice) as SellerPrice, '0.00' as amount,'' as orderNo", "vw_OrderItem_listByCategory", where, "oiid", orderby, pagesize, pageindex);
        }

        /// <summary>
        /// 应收款的订单细目
        /// </summary>
        /// <param name="where">搜索条件</param>
        /// <returns>DataSet</returns>
        public static DataSet listwithReceivables(string where)
        {
            return OrderItem.listwithReceivables(where);
        }

        /// <summary>
        /// 应付款的订单细目
        /// </summary>
        /// <param name="where">搜索条件</param>
        /// <returns>DataSet</returns>
        public static DataSet listwithPayables(string where)
        {
            return OrderItem.listwithPayables(where);
        }
        public static DataSet listWithOrder(string where, string orderby, int pagesize, int pageindex)
        {
            return DAL.DbHelper.ExecuteDataSetPageSize("*", "vw_OrderItem_listWithOrder", where, "oiid", orderby, pagesize, pageindex);
        }
        public static DataSet listWithPreview(string where, string orderby, int pagesize, int pageindex)
        {
            return DAL.DbHelper.ExecuteDataSetPageSize("*", "vw_OrderItem_listWithPreview", where, "oiid", orderby, pagesize, pageindex);
        }

        public static void updateCurrentCost(int productId, decimal sellerPrice,int adminid,string nature)
        {
            OrderItem.updateCurrentCost(productId, sellerPrice, adminid, nature);
        }

        public static bool isNotquoted(int orderid)
        {
            return OrderItem.isNotquoted(orderid);
        }

        /// <summary>
        /// 导出采购信息：商品名称、商品总采购量、总进价、总出价
        /// </summary>
        /// <param name="parentid">大类</param>
        /// <param name="categoryid">小类</param>
        /// <param name="productNo">商品编号</param>
        /// <param name="productName">商品名称</param>
        /// <param name="startDt">下单日期</param>
        /// <param name="endDt">下单日期</param>
        /// <returns></returns>
        public static DataTable listWithPurchaseInfo(int parentid, int categoryid, string productNo, string productName,DateTime? startDt, DateTime? endDt)
        {
            return OrderItem.listWithPurchaseInfo(parentid, categoryid, productNo, productName, startDt, endDt);
        }

        public static DataTable listWithPurchaseInfoOptimize(string innerWhere,string outerWhere)
        {
            return DbHelper.ExecuteDataTable("[dbo].[p_OrderItem_listWithPurchaseInfo_Optimize]", innerWhere, outerWhere);
        }
        public static DataSet listwithProductGrossprofit(string where, int pageSize, int pageIndex)
        {
            return OrderItem.listwithProductGrossprofit(where, pageSize, pageIndex);
        }
        public static DataSet listwithCategoryGrossprofit(string where, int pageSize, int pageIndex)
        {
            return OrderItem.listwithCategoryGrossprofit(where, pageSize, pageIndex);
        }

    }
}
